import csv
import numpy as np
import matplotlib.pyplot as plt # plt 用于显示图片
import os
import palettable

def to_float(mxs):
    res = []
    for mx in mxs:
        tmp = []
        for m in mx:
            tmp.append(float(m))
        res.append(tmp)
    return res

def to_float_dim1(mxs):
    res = []
    for mx in mxs:
        res.append(float(mx))

    return res

def read_csv(path):
    csvFile = open(path, "r")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        result.append(item)
    csvFile.close()
    if len(result) == 1:
        return result[0]
    return result

# 获得
def distance(arr):
    dist1 = []  #相邻两行之间的欧式距离
    dist2 = []   #每一行与第一行之间的欧式距离
    for index in range(len(arr) - 1):
        m = arr[index]
        n = arr[index + 1]
        a = np.linalg.norm(n - m)
        dist1.append(a)

        b = np.linalg.norm(arr[len(arr) - 1] - m)
        dist2.append(b)

    dist2.append(0.0)
    conti_dist = np.array(dist1)
    one_dist = np.array(dist2)

    return conti_dist, one_dist

#矩阵对应取平均
def get_mean_arr(matrix):
    return matrix.mean(axis=0)

#将两个向量画到一张图上
def pic_make(arr1, arr2, label1, label2, LB):
    x1 = []
    x2 = []
    for x in range(len(arr1)):
        x1.append(str(x))
    for x in range(len(arr2)):
        x2.append(str(x))
    plt.plot(x1, arr1, 'b', label=label1)
    plt.plot(x2, arr2, 'r', label=label2)
    plt.title(LB)
    plt.legend()

    plt.show()

# 将矩阵按行全部输出,并取平均
def pic_make_amount_curves(matrix, label):
    x = []
    for i in range(len(matrix[0])):
        x.append(str(i))
    for vec in matrix:
        plt.plot(x, vec, 'b')
    mean_vec = get_mean_arr(np.array(matrix))
    plt.plot(x, mean_vec, 'r')
    plt.title(label)

    plt.show()

def spindle_pic(matrix1, matrix2):
    vec = []
    for i in range(len(matrix1)):
        vec.append(matrix1[i][0])
    vec2 = []
    for i in range(len(matrix2)):
        vec2.append(matrix2[i][0])

    fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(12, 5))

    all_data = [vec, vec2]

    # fig = plt.figure(figsize=(8,6))

    axes.violinplot(all_data,
                    showmeans=False,
                    showmedians=True,
                    # palette=palettable.cartocolors.qualitative.Bold_9.mpl_colors
                    )
    # axes.set_title('violin plot')

    # adding horizontal grid lines

    axes.yaxis.grid(True)
    axes.set_xticks([y + 1 for y in range(len(all_data))])
    axes.set_xlabel('xlabel')
    axes.set_ylabel('ylabel')

    plt.setp(axes, xticks=[y + 1 for y in range(len(all_data))],
             xticklabels=['standard CNN', 'resnet18 KD CNN'])

    plt.show()